package com.itmck.controller;

import com.itmck.domain.User;
import com.itmck.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@Controller
@RequestMapping("user")//访问url地址。写在controller上可以进程窄化处理
public class UserController {

    @Resource
    private UserService userService;


    //最后请求路径：http://localhost:8080/ssm/user/list
    @ResponseBody
    @RequestMapping("/list")//访问url地址
    public List<User> getUserList(){

        User user = new User();
        user.setPassword("123");;
        user.setUserName("mmm");
        List<User> users = userService.queryMethod();
        log.info("users:{}",users);
        return users;
    }


    //http://localhost:8080/ssm/user/update?userName=mmm&userId=1004
    @ResponseBody
    @RequestMapping("/update")//访问url地址
    public String updateUserById(User user){
        userService.updateUserById(user);
        return "更新成功";
    }


    //http://localhost:8080/ssm/user/add?userName=mmm&password=111
    @ResponseBody
    @RequestMapping("/add")//访问url地址
    public String addUser(User user){
        userService.addUser(user);
        return "增加成功";
    }


    //http://localhost:8080/ssm/user/delete/1004
    @ResponseBody
    @RequestMapping("/delete/{id}")
    public String deleteUserById(@PathVariable("id") Long id){
        userService.deleteUserById(id);
        return "删除成功";
    }
}

